package com.example.com.test.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.com.test.R;
import com.example.com.test.adapter.MyAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者： njb
 * 时间： 2018/12/11 13:35
 * 描述：
 * 来源：
 */
public class ViewPagerActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private LinearLayout dotcontaint ;
    //ImageView动态数组
    private List<ImageView> mImageList = new ArrayList<ImageView>();
    private MyAdapter mAdapter;
    private int[] imgArray = new int[]{R.mipmap.beauty, R.mipmap.beauty, R.mipmap.beauty};
    private int mCurrentIndex = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_viewpager);
        initView();
    }

    private void initView() {
        mViewPager= findViewById(R.id.viewPager);
        dotcontaint = (LinearLayout) findViewById(R.id.ll_container);


        for (int i = 0; i < imgArray.length; i++) {
            ImageView imageView = new ImageView(this);//获取4个圆点
            imageView.setImageResource(imgArray[i]);
            mImageList.add(imageView);
            ImageView dot = new ImageView(this);
            if (i == mCurrentIndex) {
                dot.setImageResource(R.drawable.enable);//设置当前页的圆点
            } else {
                dot.setImageResource(R.drawable.disable);//其余页的圆点
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            if (i > 0) {
                params.leftMargin = 10;//设置圆点边距
            }
            dot.setLayoutParams(params);
            dotcontaint.addView(dot);//将圆点添加到容器中
        }
        mViewPager.setAdapter(new MyAdapter(mImageList));
        //添加监听
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //根据监听的页面改变当前页对应的小圆点
                mCurrentIndex = position;
                for (int i = 0; i < dotcontaint.getChildCount(); i++) {
                    ImageView imageView = (ImageView) dotcontaint.getChildAt(i);
                    if (i == position) {
                        imageView.setImageResource(R.drawable.enable);
                    } else {
                        imageView.setImageResource(R.drawable.disable);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
